package com.carkeeper.order.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.order.pojo.Order;
import com.carkeeper.order.pojo.OrderItem;
import com.carkeeper.order.service.OrderService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "服务订单")
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("分页条件查询所有订单")
    @GetMapping(value = "/search/{page}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "订单编号", required = false),
            @ApiImplicitParam(name = "contact", value = "客户名字", required = false),
            @ApiImplicitParam(name = "storeName", value = "店铺名称", required = false),
            @ApiImplicitParam(name = "createDate", value = "订单时间", required = false),
            @ApiImplicitParam(name = "payStatus", value = "支付状态", required = false),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", required = false),
            @ApiImplicitParam(name = "storeTel", value = "商家电话", required = false),
    })
    public Result findPage(@RequestParam(required = false) Map searchMap  ,@PathVariable int page , @PathVariable int size){
        Page<Order> pageList = orderService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    //按order id查询订单详细
    @ApiOperation("查询详细订单")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id",value = "订单id",required = true)
    public Result findById(@PathVariable String id){
        OrderGroup orderGroup = orderService.findByid(id);
        try {
            return new Result(true,StatusCode.OK,"查询成功",orderGroup);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        Order order = orderService.findByid(id);
        return new Result(true,StatusCode.OK,"查询成功",order);
    }

    //订单详情查询
    @GetMapping("/orderltem/{id}")
    public Result findOrderLtem(@PathVariable String id){
        OrderItem orderItem=orderService.findAll(id);
        return null;
    }

    //按orderId查询商家表
    @GetMapping("/Store/{id}")
    public Result findStoreById(@PathVariable("id") String orderId){
        Result result = orderService.findStoreById(orderId);
        return result;

    }

}
